package com.baselibrary.libs.design_mode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.baselibrary.libs.R;
import com.baselibrary.libs.Utils.log.LogUtils;
import com.baselibrary.libs.design_mode.Observable_demo.Appc;
import com.baselibrary.libs.design_mode.Observable_demo.Observer.base_P;
import com.baselibrary.libs.design_mode.Observable_demo.bean.BBBB;
import com.baselibrary.libs.design_mode.Observable_demo.ui.MainActivity1;
import com.baselibrary.libs.design_mode.Observable_demo.ui.MainActivity2;

public class DesignModeActivity extends Activity implements View.OnClickListener {

    private Button bt_01;
    private Button bt_02;
    private EditText edit;
    private Button bt_03;
    private RelativeLayout activity_design_mode;
    private base_P mP_b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_design_mode);
        mP_b = new base_P<BBBB>();
        Appc.sGetData().addObserver(mP_b);
        initView();

    }


    private void initView() {
        bt_01 = (Button) findViewById(R.id.bt_01);
        bt_02 = (Button) findViewById(R.id.bt_02);
        edit = (EditText) findViewById(R.id.edit);
        bt_03 = (Button) findViewById(R.id.bt_03);
        activity_design_mode = (RelativeLayout) findViewById(R.id.activity_design_mode);

        bt_01.setOnClickListener(this);
        bt_02.setOnClickListener(this);
        bt_03.setOnClickListener(this);

//        mP_b.setCallBack(BBBB.class, new base_P.CallBackBean<BBBB>() {
//            @Override
//            public void callback(BBBB o) {
//                LogUtils.i("得到的结果是BBBB" + o);
//            }
//        });
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.bt_01) {
            startActivity(new Intent(this,MainActivity1.class));
        } else if (i == R.id.bt_02) {
            startActivity(new Intent(this,MainActivity2.class));
        } else if (i == R.id.bt_03) {
            BBBB bbbb = new BBBB();
            bbbb.setName(edit.getText().toString());
            Appc.sGetData().setT(bbbb);
            Appc.sGetData().setData(edit.getText().toString());
            LogUtils.i("有几个响应到了"+Appc.sGetData().countObservers());
            BBBB t = (BBBB)mP_b.getT(BBBB.class);
            LogUtils.i("内容是"+t);
        }
    }

    private void submit() {
        // validate
        String editString = edit.getText().toString().trim();
        if (TextUtils.isEmpty(editString)) {
            Toast.makeText(this, "输入修改内容", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something


    }
}
